package me.xhy.java.springboot3.webflux.s03exceptionandparam;

import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.reactive.result.view.Rendering;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebSession;
import reactor.core.publisher.Mono;

// RestController 限制了视图解析器。不能使用 Rendering
@Controller
public class S033Controller {

  // http://localhost:8080/errorWhen0?value=0
  @ResponseBody
  @GetMapping("errorWhen0")
  public Mono<String> errorWhen0(@RequestParam(value = "value", required = false, defaultValue = "10") int value,
                             ServerWebExchange exchange,
                             WebSession session,
                             HttpMethod method) {
    return Mono.just(value)
        .map(i -> 10 / value)
        .map(String::valueOf);
  }

  @RequestMapping("/rendering")
  public Rendering rendering() {
    // return Rendering.redirectTo("/a").build(); // 返回到根目录下的资源，可以配合模板引擎
    return Rendering.redirectTo("http://www.baidu.com").build();
  }



}
